import 'package:flutter_shopcar_test/database/sqlite_data.dart';
import 'package:flutter_shopcar_test/model/index.dart';
import 'package:sqflite/sqflite.dart';

class MethodDao {

  static MethodDao? _instance;
  static MethodDao getInstance() => _instance ??= MethodDao();

  /// 商品插入
  static Future<int> insertGoodsList(GoodsListModel values) async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    return await db.insert(SqliteDataManger.goodsTableName, values.toJson());
  }

  /// 查询所有商品
  static Future<List<GoodsListModel>> queryGoodsList() async {
    List<GoodsListModel> list = [];
    Database db = await SqliteDataManger.getInstance().getDatabase;
    List<Map<String, dynamic>> records = await db.query(SqliteDataManger.goodsTableName);
    print("查询所有商品:$records");
    if(records.isNotEmpty) {
      for (var value in records) {
        GoodsListModel model = GoodsListModel.fromJson(value);
        list.add(model);
      }
      return list;
    }
    return [];
  }

  /// 更新商品
  static Future<int> updateGoodsList(GoodsListModel values) async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    return db.update(SqliteDataManger.goodsTableName, values.toJson(), where: 'id=?', whereArgs: [values.id]);
  }

  /// 购物车商品插入
  static Future<int> insertCartGoodsList(CartModel values) async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    return await db.insert(SqliteDataManger.shopCartTable, values.toJson());
  }

  /// 查询所有购物车商品
  static Future<List<CartModel>> queryCartGoodsList() async {
    List<CartModel> list = [];
    Database db = await SqliteDataManger.getInstance().getDatabase;
    List<Map<String, dynamic>> records = await db.query(SqliteDataManger.shopCartTable);
    print("查询所有购物车商品:$records");
    if(records.isNotEmpty) {
      for (var value in records) {
        CartModel model = CartModel.fromJson(value);
        list.add(model);
      }
      return list;
    }
    return [];
  }

  /// 修改购物车商品
  static Future<int> updateCartGoodsList(CartModel values) async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    return db.update(SqliteDataManger.shopCartTable, values.toJson(), where: 'id=?', whereArgs: [values.id]);
  }

  /// 清空购物车
  static Future<int> deleteAllCartGoodsList() async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    return await db.delete(SqliteDataManger.shopCartTable);
  }

  static Future<int> deleteCartGoodsList(CartModel values) async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    return await db.delete(SqliteDataManger.shopCartTable, where: 'id = ?', whereArgs: [values.id]);
  }

  /// 插入order
  static Future<int> insertOrder(OrderModel values) async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    return db.insert(SqliteDataManger.orderTable, values.toJson());
  }

  /// 查询订单
  static Future<List<OrderModel>> queryOrderList() async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    List<OrderModel> list = [];
    List<Map<String, dynamic>> records = await db.query(SqliteDataManger.orderTable);
    print('=======查询订单未关联子明细========:$records');
    if(records.isNotEmpty) {
      for (var e in records) {
        OrderModel model = OrderModel.fromJson(e);
        list.add(model);
      }
    }
    return list;
  }

  static Future<List<OrderListModel>> queryAllOrderList() async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    List<OrderListModel> list = [];
    List<Map<String, dynamic>> records = await db.rawQuery(SqliteDataManger.sqlOrder);
    print('---------查询订单---------:$records');
    return [];
  }

  /// 插入订单明细
  static Future<int> insertOrderItem(OrderItemModel values) async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    return db.insert(SqliteDataManger.orderItemTable, values.toJson());
  }

  /// 查询订单明细
  static Future<List<OrderItemModel>> queryOrderItemList() async {
    Database db = await SqliteDataManger.getInstance().getDatabase;
    List<OrderItemModel> list = [];
    List<Map<String, dynamic>> records = await db.query(SqliteDataManger.orderItemTable);
    print('=======查询订单子明细========:$records');
    if(records.isNotEmpty) {
      for (var e in records) {
        OrderItemModel model = OrderItemModel.fromJson(e);
        list.add(model);
      }
    }
    return list;
  }

}